import type { BusinessParams, SectionDelayParams, SystemConfig } from './types'
import LatController from './lat-controller'

let systemConfig: Required<SystemConfig>

const defaultSystemConfig: Partial<SystemConfig> = {

}

const defaultBusinessParams: Partial<BusinessParams> = {
  language: 'zh_cn',
  domain: 'iat',
  accent: 'mandarin',
  vad_eos: 3600000,
  dwa: 'wpgs',
}

const defaultSectionDelayParams: Partial<SectionDelayParams> = {
  initialDelay: 3500,
  subsequentDelay: 3000,
  autoControl: true,
}

/**
 * 用于管理语音转文字配置
 */
export default {
  /**
   * 配置系统参数。
   * @param _systemConfig - 系统配置参数。
   * @returns 返回当前对象，支持链式调用。
   */
  config(_systemConfig: Required<SystemConfig>) {
    systemConfig = {
      ...defaultSystemConfig,
      ..._systemConfig,
    }

    return this
  },
  create(
    _businessParams: Partial<BusinessParams> = {},
    _sectionDelayParams: Partial<SectionDelayParams> = {},
  ): LatController {
    const { API_KEY, API_SECRET, APPID } = systemConfig
    if (!systemConfig) {
      throw new Error('请先调用config方法进行配置')
    }
    if (!API_SECRET) {
      throw new Error('API_SECRET不能为空')
    }
    if (!APPID) {
      throw new Error('APPID不能为空')
    }
    if (!API_KEY) {
      throw new Error('API_KEY不能为空')
    }

    const businessParams = {
      ...defaultBusinessParams,
      ..._businessParams,
    } as Required<BusinessParams>
    const sectionDelayParams = {
      ...defaultSectionDelayParams,
      ..._sectionDelayParams,
    } as Required<SectionDelayParams>

    return new LatController(systemConfig, businessParams, sectionDelayParams)
  },
}
